package com.wangtao.firstapp.fragments.homefragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.TakePhotoActivity;
import com.wangtao.firstapp.adapters.HomeVPAdapter;
import com.wangtao.firstapp.adapters.MajorVpAdapter;
import com.wangtao.firstapp.fragments.communityfragment.CommunityArticleFragment;
import com.wangtao.firstapp.fragments.communityfragment.CommunityShowFragment;

import java.util.ArrayList;

import me.iwf.photopicker.PhotoPickerActivity;

/**
 * 第二主页面
 * Created by lanouhn on 16/8/24.
 */
public class CommunityFragment extends Fragment {
    private TabLayout tabLayout;// TabLayout控件
    private ArrayList<String> tabnames;// 存放TabLayout菜单的名字
    private ArrayList<Fragment> fragments;// 存放Fragment的集合
    private View view;
    private MajorVpAdapter majorVpAdapter;
    private ViewPager viewpager;
    private HomeVPAdapter homeVPAdapter;
    private ImageView iv_community_titlebar_pen, iv_community_titlebar_camera;// 文章界面TabLayout右侧图标控件


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.communityfragment, null);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_community_top);
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        iv_community_titlebar_pen = (ImageView) view.findViewById(R.id.iv_community_titlebar_pen);
        iv_community_titlebar_camera = (ImageView) view.findViewById(R.id.iv_community_titlebar_camera);
        initData();
        initEvent();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        iv_community_titlebar_pen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_write = new Intent(getActivity(), com.wangtao.firstapp.activity.stairactivity.WriteArticleActivity.class);
                startActivity(intent_write);
            }
        });
        iv_community_titlebar_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_takephoto = new Intent(getActivity(), PhotoPickerActivity.class);
                startActivity(intent_takephoto);
            }
        });
    }

    private void initData() {
        tabnames = new ArrayList<>();
        tabnames.add("文章");
        tabnames.add("晒单");
        fragments = new ArrayList<>();
        final CommunityArticleFragment communityArticleFragment = new CommunityArticleFragment();
        fragments.add(communityArticleFragment);
        final CommunityShowFragment communityShowFragment = new CommunityShowFragment();
        fragments.add(communityShowFragment);
        homeVPAdapter = new HomeVPAdapter(getFragmentManager(), tabnames, fragments);
        viewpager.setAdapter(homeVPAdapter);
        tabLayout.setupWithViewPager(viewpager);
    }

    private void initEvent() {

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            // 滑动Fragment时右上角图标转换
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        iv_community_titlebar_pen.setVisibility(View.VISIBLE);
                        iv_community_titlebar_camera.setVisibility(View.INVISIBLE);
                        viewpager.setCurrentItem(0);
                        break;
                    case 1:
                        iv_community_titlebar_pen.setVisibility(View.INVISIBLE);
                        iv_community_titlebar_camera.setVisibility(View.VISIBLE);
                        viewpager.setCurrentItem(1);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
